package com.hy.formula.common;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class BigDecimalUtils {

	public final static BigDecimal ZERO = new BigDecimal(0);
	public final static BigDecimal ONE = new BigDecimal(1);
	
	
	public static BigDecimal getBigDecimal(int num){
		return getMoney(String.valueOf(num));
	}
	
	public static BigDecimal getBigDecimal(float num){
		return getMoney(String.valueOf(num));
	}
	
	public static BigDecimal getBigDecimal(double num){
		return getMoney(String.valueOf(num));
	}
	
	public static BigDecimal getBigDecimal(String num){
		return getMoney(num);
	}
	
	/**
	 * 四舍五入获取金额
	 * @param money
	 * @return
	 */
	private static BigDecimal getMoney(String money) {
		BigDecimal m = new BigDecimal(Double.parseDouble(money));
		BigDecimal one = new BigDecimal("1");
		return m.divide(one, 2, BigDecimal.ROUND_HALF_UP);
	}
	
	public static BigDecimal round(BigDecimal num,int scale){
		return num.divide(ONE, 2, BigDecimal.ROUND_HALF_UP);
	}

	/**
	 * 
	 * @param one
	 * @param two
	 * @return
	 */
	public static BigDecimal multi(BigDecimal one, BigDecimal two) {
		if(one == null){
			return new BigDecimal(0);
		}
		if(two == null){
			return new BigDecimal(0);
		}
		return round(one.multiply(two),2);
	}
	
	public static int multi100(BigDecimal one){
		return multi(one, new BigDecimal(100)).intValue();
	}
	
	public static String toMoneyStr(BigDecimal num) {
		DecimalFormat dFormat = new DecimalFormat("0.00");
		return dFormat.format(num);
	}

	public static Object div(BigDecimal one, BigDecimal two) {
		if(one == null){
			return new BigDecimal(0);
		}
		if(two == null){
			return new BigDecimal(0);
		}
		return round(one.divide(two),2);
	}

	public static BigDecimal sum(BigDecimal one, BigDecimal two) {
		if(one == null && two == null) {
			return BigDecimal.ZERO;
		}
		if(one == null) {
			return two;
		}
		if(two == null) {
			return one;
		}
		return one.add(two);
	}
}
